--
-- @Author:      name
-- @DateTime:    2018-03-30 23:05:48
-- @Description: 后台消息的处理

local skynet = require "skynet"
local config = require "configquery"
local NodeMessage = require "NodeMessage"
local Timer = require "timer"
require "common"

local AssertEx = AssertEx
local Command = class("Command")

---------------------------------------------------------
-- Private
---------------------------------------------------------
function Command:ctor(message)
	self.msg = message
	self.nodeMessage = NodeMessage.new()
	self.timer = Timer:new()
	self.timeHandler = nil

	self:register()
	self:test()	
end

--注册本服务里的消息
function Command:register()

	self.msg:registerSelf('web_bulletin',handler(self,self.bulletin))
	self.msg:registerSelf('web_niuniu_card',handler(self,self.niuniuCard))
	self.msg:registerSelf('web_change_money',handler(self,self.changeMoney))
	self.msg:registerSelf('web_change_money2',handler(self,self.changeMoney2))
	self.msg:registerSelf('web_broadcast',handler(self,self.broadcast))	
	self.msg:registerSelf('web_change_config',handler(self,self.changeConfig))		
	self.msg:registerSelf('web_safeclose',handler(self,self.safeClose))	

end


---------------------------------------------------------
-- req
---------------------------------------------------------


-- --封号
-- --禁言
-- --封IP
-- --封mac


--后台对用户属性进行修改
function Command:changeMoney( args )
	--print("-------------------------------------------php nt changeMoney:", args)
	if args.uid then
		self.nodeMessage:sendService('.proxy','send_gate',args.uid,'web_change_money', args)
	end
	return {}
end

--后台对用户属性进行修改
function Command:changeMoney2( args )
	print("-------------------------------------------php nt changeMoney:", args)
	local res
	
	if not args or not args.uid then 
		return 
	end
	local uid = tonumber(args.uid)
	local game = tonumber(args.game)
	local code = tonumber(args.code)
	local moneyType = args.moneytype
	local moneyFlag = args.moneyFlag
	local money = args.money	
	args.uid = uid
	if uid >0 then
		res = self.nodeMessage:callService('.proxy','call_gate',args.uid,'web_change_money2', args)			
		print("___玩家在线__",res)
		--玩家不在线
		if not res then 
			local resMoney = 0
			local data
			local goodName
			if moneyFlag == 0 then 
				moneyFlag = 1
			elseif moneyFlag == 1 then 
				moneyFlag = -1
			end
			money = money*moneyFlag	
			local name 	
		    if moneyType==1 then 
		    	name = "uchip"
		    elseif moneyType==2 then 
		  		name = "udiamond"
		    elseif moneyType==3 then		   	
			elseif moneyType==4 then   
				name = "room_ticket"
			end				
			local sql = string.format("update usergame set %s=%s+%d where uid=%d",name,name,money,uid)
			res = self.nodeMessage:callService("mysql_service","execute", sql)
			
		end		
	end
	local success = 1
	if res then 
		success = 0
		--流水记录写到		
		local tableName = 'coinlog'.. os.date("%Y%m%d", os.time())
		local str = tableName .." (uid,changecoin,time,code,moneytype,game,gameid,gametype)"
		local sql = string.format("insert into %s value(%d,%d,%d,%d,%d,%d,%d,%d)",str,uid,money,os.time(),code,moneyType,game,0,0)
		skynet.send("mysql_log", "lua", "execute", sql)		
	end
	return {nopid={code=success}}
end

--牛牛配牌
function Command:niuniuCard( args )
	local tbRes = {}
	local mid = 0
	local tableid = 0
	for k, v in pairs(args) do 
		mid = v.mid
		tableid = 0
		if mid and mid > 0 then 
		 	tableid = self.nodeMessage:callService('.proxy','call_gate',mid,'get_user_table')
		 	print("__________###tableid___",tableid)
		end
		if not tableid or tableid <= 0 then 
			table.insert(tbRes,{res = 0,mid = mid,})			
		elseif not v.card or not next(v.card) then 			
			table.insert(tbRes,{res = 0,mid = mid,})			
		else		
			self.nodeMessage:sendTable(tableid, 'gm_set_code_req', mid,v.card,v.pokertype)
			table.insert(tbRes,{res = 1,mid = mid,})
		end
			
	end
	print("__________###tbRes___",tbRes)
	return {web_niuniu_card=tbRes}
end

--公告
function Command:bulletin(args)
	local mid = args.mid
	local msg = args.msg
	local cmd = args.cmd
	if not mid then return end
 	local data = {
		php_res = {
			id = cmd,
		 	content = msg,
		 }
	}	

	if mid == 0 then 
		--给所有人
		self.nodeMessage:sendService('bulletin','add_broadcast',data)
	else
		--给指定的人
		self.nodeMessage:sendService('.proxy','send_user',mid,data)
	end	
	return {web_bulletin = {mid=mid,res="1"}}
end

--跑马灯
function Command:broadcast(args)
	local mid = args.mid
	local msg = args.msg
	local id = args.id
	local cmd = args.cmd
	if not mid then
		return 
	end
	if type(msg) ~= 'table' then 
		return {web_broadcast = {mid=mid,res="0"}}
	end
	local num = msg.num 
	local content = msg.content
 	local data = {
		nt_broadcast = {
			id = id,
		 	content = content,
		 }
	}	
	-- print("____________args___1",id,mid)
	for i=1,num do 
		if id == 1 then --常规跑马灯
			if mid == 0 then 
				self.nodeMessage:sendService('bulletin','add_broadcast',data)
			else
				self.nodeMessage:sendService('.proxy','send_user',mid,data)
			end	
		elseif id == 2 then --紧急跑马灯
			if mid == 0 then 
				self.nodeMessage:sendService('bulletin','add_broadcast',data)
			else
				self.nodeMessage:sendService('.proxy','send_user',mid,data)
			end			
		end
	end
	return {web_broadcast = {mid=mid,res="1"}}
end

--修改配置
function Command:changeConfig(args)
	
	local msg = args.msg
	local cmd = args.cmd	
	-- print("__________type(msg)",type(msg))
	if not msg then return end
 	local data = {
		php_res = {
			id = cmd,
		 	content = msg,
		 }
	}	
	-- print("data ______________",data)
	skynet.call('bulletin','lua','add_broadcast',data)
	return {web_change_config = {res="1"}}
end

--安全关服
function Command:safeClose(args)

	-- state: 1 //1关服， 0取消关服
	-- time: 1232443 //关服时间
	-- msg: ""   //内容
	-- gameid:0 //0全服 100牛牛

	-- 1.开启定时器，时间到后发通知
	-- 2.通知gate manager 如果关全服务不给登录
	-- 3.通知game manager 关掉所有游戏或指定游戏
	-- 4.推跑马灯		
  	local close = false
  	local time = tonumber(args.time)
  	local msg = args.msg
  	local gameid = args.gameid
  	local closeGame = false
  	local closeGate = false
	if args.state == 1 then 
		close = true
	end      
	if self.timeHandler then 
		self.timer:unregister(self.timeHandler)
		self.timeHandler = nil
	end
	--全服开启
	if close == false and gameid == 0 then 
		print('____________开全服时间___',os.date("%Y-%m-%d %H:%M:%S", os.time()))
		self.nodeMessage:sendAllGateManager('safe_close',close,gameid)
		self.nodeMessage:sendAllGameManager('safe_close',close,gameid)
		return
	end
	local date = "" .. os.date("%Y-%m-%d %H:%M:%S", time)
	print('____________设置时间___',date)
	print('____________系统时间___',os.date("%Y-%m-%d %H:%M:%S", os.time()))
	if time < os.time() then 
		print("_____安全关服____时间设置不正确——————")
		return 
	end

	local function sendPaoMaDeng()
		--关服跑马灯 
	 	local data = {
			nt_broadcast = {
				id = 2,
			 	content = msg,
			 }
		}    
		self.nodeMessage:sendService('bulletin','add_broadcast',data)
	end
	sendPaoMaDeng()
	local callback = function ()
		--关游戏
		if os.time() > time and not closeGame then 
			print('____________开关游戏时间___',os.date("%Y-%m-%d %H:%M:%S", os.time()),gameid)
			self.nodeMessage:sendAllGameManager('safe_close',close,gameid)			
			closeGame = true
		end
		--10分钟后关大厅,如果游戏关完会通知大厅提前关闭
		if gameid == 0 then 
			if os.time() > time + 10*60 and not closeGate then 
				print('____________关全服时间___',os.date("%Y-%m-%d %H:%M:%S", os.time()))
				self.nodeMessage:sendAllGateManager('safe_close',close,gameid)		
				closeGate = true	
			end
		end
		--关闭定时器
		if os.time() > time + 3*60  then 
			self.timer:unregister(self.timeHandler)
			self.timeHandler = nil
			return			
		end		
		--关服,发一条跑马灯 
	 	sendPaoMaDeng()
	end
	self.timeHandler = self.timer:register(10,nil,callback,true)
end










--自己测试
function Command:test()
	skynet.fork(function()
		skynet.sleep(800)

		--修改属性
		-- local data = {
		-- 	{mid=10000937,status=1}
		-- }
		-- print("______________1")
		-- self:changeMoney(data)

		-- --安全关服
		-- local args = {
		-- 	time = os.time() + 2*60,
		-- 	msg = "准备关服了——————",
		-- 	gameid = 0,
		-- 	state = 1,

		-- }
		-- self:safeClose(args)	

		-- 修改属性
 	-- $sendArr['uid'] = $uid;
  --   $sendArr['money'] = $money;
  --   $sendArr['code'] = $code;
  --   $sendArr['moneytype'] = $this->type[$type];
  --   $sendArr['moneyFlag'] = $moneyFlag;
  --   $sendArr['msgtype'] = 1;		
		local data = 
		{
			uid=5000295,
			money=100,
			code=1,
			moneytype=4,
			moneyFlag=1,
			msgtype=1,
		}
		
		-- print("______________1")
		-- self:changeMoney2(data)
	end)
end

return Command